<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
  <link rel="shortcut icon" href="../../img/favicon.ico">
  <title>序列预处理 - Keras 中文文档</title>
  <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>

  <link rel="stylesheet" href="../../css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../css/theme_extra.css" type="text/css" />
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
  
  <script>
    // Current page data
    var mkdocs_page_name = "\u5e8f\u5217\u9884\u5904\u7406";
    var mkdocs_page_input_path = "preprocessing/sequence.md";
    var mkdocs_page_url = "/zh/preprocessing/sequence/";
  </script>
  
  <script src="../../js/jquery-2.1.1.min.js" defer></script>
  <script src="../../js/modernizr-2.8.3.min.js" defer></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
  <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-61785484-1', 'keras.io');
      ga('send', 'pageview');
  </script>
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
      <div class="wy-side-nav-search">
        <a href="../.." class="icon icon-home"> Keras 中文文档</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
	<ul class="current">
	  
          
            <li class="toctree-l1">
		
    <a class="" href="../..">主页</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../why-use-keras/">为什么选择 Keras?</a>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">快速开始</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../getting-started/sequential-model-guide/">Sequential 顺序模型指引</a>
                </li>
                <li class="">
                    
    <a class="" href="../../getting-started/functional-api-guide/">函数式 API 指引</a>
                </li>
                <li class="">
                    
    <a class="" href="../../getting-started/faq/">FAQ 常见问题解答</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">模型</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../models/about-keras-models/">关于 Keras 模型</a>
                </li>
                <li class="">
                    
    <a class="" href="../../models/sequential/">Sequential 顺序模型 API</a>
                </li>
                <li class="">
                    
    <a class="" href="../../models/model/">函数式 API</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Layers</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../layers/about-keras-layers/">关于 Keras 网络层</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/core/">核心网络层</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/convolutional/">卷积层 Convolutional</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/pooling/">池化层 Pooling</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/local/">局部连接层 Locally-connected</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/recurrent/">循环层 Recurrent</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/embeddings/">嵌入层 Embedding</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/merge/">融合层 Merge</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/advanced-activations/">高级激活层 Advanced Activations</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/normalization/">标准化层 Normalization</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/noise/">噪声层 Noise</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/wrappers/">层封装器 wrappers</a>
                </li>
                <li class="">
                    
    <a class="" href="../../layers/writing-your-own-keras-layers/">编写你自己的层</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">数据预处理</span>
    <ul class="subnav">
                <li class=" current">
                    
    <a class="current" href="./">序列预处理</a>
    <ul class="subnav">
            
    <li class="toctree-l3"><a href="#timeseriesgenerator">TimeseriesGenerator</a></li>
    

    <li class="toctree-l3"><a href="#pad_sequences">pad_sequences</a></li>
    

    <li class="toctree-l3"><a href="#skipgrams">skipgrams</a></li>
    

    <li class="toctree-l3"><a href="#make_sampling_table">make_sampling_table</a></li>
    

    </ul>
                </li>
                <li class="">
                    
    <a class="" href="../text/">文本预处理</a>
                </li>
                <li class="">
                    
    <a class="" href="../image/">图像预处理</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../losses/">损失函数 Losses</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../metrics/">评估标准 Metrics</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../optimizers/">优化器 Optimizers</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../activations/">激活函数 Activations</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../callbacks/">回调函数 Callbacks</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../datasets/">常用数据集 Datasets</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../applications/">应用 Applications</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../backend/">后端 Backend</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../initializers/">初始化 Initializers</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../regularizers/">正则化 Regularizers</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../constraints/">约束 Constraints</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../visualization/">可视化 Visualization</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../scikit-learn-api/">Scikit-learn API</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../utils/">工具</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../../contributing/">贡献</a>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">经典样例</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../../examples/addition_rnn/">Addition RNN</a>
                </li>
                <li class="">
                    
    <a class="" href="../../examples/babi_rnn/">Baby RNN</a>
                </li>
                <li class="">
                    
    <a class="" href="../../examples/babi_memnn/">Baby MemNN</a>
                </li>
                <li class="">
                    
    <a class="" href="../../examples/cifar10_cnn/">CIFAR-10 CNN</a>
                </li>
                <li class="">
                    
    <a class="" href="../../examples/cifar10_cnn_capsule/">CIFAR-10 CNN-Capsule</a>
                </li>
                <li class="">
                    
    <a class="" href="../../examples/cifar10_cnn_tfaugment2d/">CIFAR-10 CNN with augmentation (TF)</a>
                </li>
                <li class="">
                    
    <a class="" href="../../examples/cifar10_resnet/">CIFAR-10 ResNet</a>
                </li>
                <li class="">
                    
    <a class="" href="../../examples/conv_filter_visualization/">Convolution filter visualization</a>
                </li>
                <li class="">
                    
    <a class="" href="../../examples/image_ocr/">Image OCR</a>
                </li>
                <li class="">
                    
    <a class="" href="../../examples/imdb_bidirectional_lstm/">Bidirectional LSTM</a>
                </li>
    </ul>
	    </li>
          
        </ul>
      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../..">Keras 中文文档</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../..">Docs</a> &raquo;</li>
    
      
        
          <li>数据预处理 &raquo;</li>
        
      
    
    <li>序列预处理</li>
    <li class="wy-breadcrumbs-aside">
      
        <a href="https://github.com/keras-team/keras-docs-zh/edit/master/docs/preprocessing/sequence.md"
          class="icon icon-github"> Edit on GitHub</a>
      
    </li>
  </ul>
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <p><span style="float:right;"><a href="https://github.com/keras-team/keras/blob/master/keras/preprocessing/sequence.py#L16">[source]</a></span></p>
<h3 id="timeseriesgenerator">TimeseriesGenerator</h3>
<pre><code class="python">keras.preprocessing.sequence.TimeseriesGenerator(data, targets, length, sampling_rate=1, stride=1, start_index=0, end_index=None, shuffle=False, reverse=False, batch_size=128)
</code></pre>

<p>用于生成批量时序数据的实用工具类。</p>
<p>这个类以一系列由相等间隔以及一些时间序列参数（例如步长、历史长度等）汇集的数据点作为输入，以生成用于训练/验证的批次数据。</p>
<p><strong>参数</strong></p>
<ul>
<li><strong>data</strong>: 可索引的生成器（例如列表或 Numpy 数组），包含连续数据点（时间步）。数据应该是 2D 的，且第 0 个轴为时间维度。</li>
<li><strong>targets</strong>: 对应于 <code>data</code> 的时间步的目标值。它应该与 <code>data</code> 的长度相同。</li>
<li><strong>length</strong>: 输出序列的长度（以时间步数表示）。</li>
<li><strong>sampling_rate</strong>: 序列内连续各个时间步之间的周期。对于周期 <code>r</code>, 时间步 <code>data[i]</code>, <code>data[i-r]</code>, ... <code>data[i - length]</code> 被用于生成样本序列。</li>
<li><strong>stride</strong>: 连续输出序列之间的周期. 对于周期 <code>s</code>, 连续输出样本将为 <code>data[i]</code>, <code>data[i+s]</code>, <code>data[i+2*s]</code> 等。</li>
<li><strong>start_index</strong>: 在 <code>start_index</code> 之前的数据点在输出序列中将不被使用。这对保留部分数据以进行测试或验证很有用。</li>
<li><strong>end_index</strong>: 在 <code>end_index</code> 之后的数据点在输出序列中将不被使用。这对保留部分数据以进行测试或验证很有用。</li>
<li><strong>shuffle</strong>: 是否打乱输出样本，还是按照时间顺序绘制它们。</li>
<li><strong>reverse</strong>: 布尔值: 如果 <code>true</code>, 每个输出样本中的时间步将按照时间倒序排列。</li>
<li><strong>batch_size</strong>: 每个批次中的时间序列样本数（可能除最后一个外）。</li>
</ul>
<p><strong>返回</strong></p>
<p>一个 <a href="https://keras.io/zh/utils/#sequence">Sequence</a> 实例。</p>
<p><strong>例子</strong></p>
<pre><code class="python">from keras.preprocessing.sequence import TimeseriesGenerator
import numpy as np

data = np.array([[i] for i in range(50)])
targets = np.array([[i] for i in range(50)])

data_gen = TimeseriesGenerator(data, targets,
                               length=10, sampling_rate=2,
                               batch_size=2)
assert len(data_gen) == 20

batch_0 = data_gen[0]
x, y = batch_0
assert np.array_equal(x,
                      np.array([[[0], [2], [4], [6], [8]],
                                [[1], [3], [5], [7], [9]]]))
assert np.array_equal(y,
                      np.array([[10], [11]]))
</code></pre>

<hr />
<h3 id="pad_sequences">pad_sequences</h3>
<pre><code class="python">keras.preprocessing.sequence.pad_sequences(sequences, maxlen=None, dtype='int32', padding='pre', truncating='pre', value=0.0)
</code></pre>

<p>将多个序列截断或补齐为相同长度。</p>
<p>该函数将一个 <code>num_samples</code> 的序列（整数列表）转化为一个 2D Numpy 矩阵，其尺寸为 <code>(num_samples, num_timesteps)</code>。 <code>num_timesteps</code> 要么是给定的 <code>maxlen</code> 参数，要么是最长序列的长度。</p>
<p>比 <code>num_timesteps</code> 短的序列将在末端以 <code>value</code> 值补齐。</p>
<p>比 <code>num_timesteps</code> 长的序列将会被截断以满足所需要的长度。补齐或截断发生的位置分别由参数 <code>pading</code> 和 <code>truncating</code> 决定。</p>
<p>向前补齐为默认操作。</p>
<p><strong>参数</strong></p>
<ul>
<li><strong>sequences</strong>: 列表的列表，每一个元素是一个序列。</li>
<li><strong>maxlen</strong>: 整数，所有序列的最大长度。</li>
<li><strong>dtype</strong>: 输出序列的类型。
要使用可变长度字符串填充序列，可以使用 <code>object</code>。</li>
<li><strong>padding</strong>: 字符串，'pre' 或 'post' ，在序列的前端补齐还是在后端补齐。</li>
<li><strong>truncating</strong>: 字符串，'pre' 或 'post' ，移除长度大于 <code>maxlen</code> 的序列的值，要么在序列前端截断，要么在后端。</li>
<li><strong>value</strong>: 浮点数，表示用来补齐的值。</li>
</ul>
<p><strong>返回</strong></p>
<ul>
<li><strong>x</strong>: Numpy 矩阵，尺寸为 <code>(len(sequences), maxlen)</code>。</li>
</ul>
<p><strong>异常</strong></p>
<ul>
<li>ValueError: 如果截断或补齐的值无效，或者序列条目的形状无效。</li>
</ul>
<hr />
<h3 id="skipgrams">skipgrams</h3>
<pre><code class="python">keras.preprocessing.sequence.skipgrams(sequence, vocabulary_size, window_size=4, negative_samples=1.0, shuffle=True, categorical=False, sampling_table=None, seed=None)
</code></pre>

<p>生成 skipgram 词对。</p>
<p>该函数将一个单词索引序列（整数列表）转化为以下形式的单词元组：</p>
<ul>
<li>（单词, 同窗口的单词），标签为 1（正样本）。</li>
<li>（单词, 来自词汇表的随机单词），标签为 0（负样本）。</li>
</ul>
<p>若要了解更多和 Skipgram 有关的知识，请参阅这份由 Mikolov 等人发表的经典论文： <a href="http://arxiv.org/pdf/1301.3781v3.pdf">Efficient Estimation of Word Representations in Vector Space</a></p>
<p><strong>参数</strong></p>
<ul>
<li><strong>sequence</strong>: 一个编码为单词索引（整数）列表的词序列（句子）。如果使用一个 <code>sampling_table</code>，词索引应该以一个相关数据集的词的排名匹配（例如，10 将会编码为第 10 个最长出现的词）。注意词汇表中的索引 0 是非单词，将被跳过。</li>
<li><strong>vocabulary_size</strong>: 整数，最大可能词索引 + 1</li>
<li><strong>window_size</strong>: 整数，采样窗口大小（技术上是半个窗口）。词 <code>w_i</code> 的窗口是 <code>[i - window_size, i + window_size+1]</code>。</li>
<li><strong>negative_samples</strong>: 大于等于 0 的浮点数。0 表示非负（即随机）采样。1 表示与正样本数相同。</li>
<li><strong>shuffle</strong>: 是否在返回之前将这些词语打乱。</li>
<li><strong>categorical</strong>: 布尔值。如果 False，标签将为整数（例如 <code>[0, 1, 1 .. ]</code>），如果 True，标签将为分类，例如 <code>[[1,0],[0,1],[0,1] .. ]</code>。</li>
<li><strong>sampling_table</strong>: 尺寸为 <code>vocabulary_size</code> 的 1D 数组，其中第 i 项编码了排名为 i 的词的采样概率。</li>
<li><strong>seed</strong>: 随机种子。</li>
</ul>
<p><strong>返回</strong></p>
<p>couples, labels: 其中 <code>couples</code> 是整数对，<code>labels</code> 是 0 或 1。</p>
<p><strong>注意</strong></p>
<p>按照惯例，词汇表中的索引 0 是非单词，将被跳过。</p>
<hr />
<h3 id="make_sampling_table">make_sampling_table</h3>
<pre><code class="python">keras.preprocessing.sequence.make_sampling_table(size, sampling_factor=1e-05)
</code></pre>

<p>生成一个基于单词的概率采样表。</p>
<p>用来生成 <code>skipgrams</code> 的 <code>sampling_table</code> 参数。<code>sampling_table[i]</code> 是数据集中第 i 个最常见词的采样概率（出于平衡考虑，出现更频繁的词应该被更少地采样）。</p>
<p>采样概率根据 word2vec 中使用的采样分布生成：</p>
<pre><code class="python">p(word) = (min(1, sqrt(word_frequency / sampling_factor) /
    (word_frequency / sampling_factor)))
</code></pre>

<p>我们假设单词频率遵循 Zipf 定律（s=1），来导出 frequency(rank) 的数值近似：</p>
<p><code>frequency(rank) ~ 1/(rank * (log(rank) + gamma) + 1/2 - 1/(12*rank))</code>，其中 <code>gamma</code> 为 Euler-Mascheroni 常量。</p>
<p><strong>参数</strong></p>
<ul>
<li><strong>size</strong>: 整数，可能采样的单词数量。</li>
<li><strong>sampling_factor</strong>: word2vec 公式中的采样因子。</li>
</ul>
<p><strong>返回</strong></p>
<p>一个长度为 <code>size</code> 大小的 1D Numpy 数组，其中第 i 项是排名为 i 的单词的采样概率。</p>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../text/" class="btn btn-neutral float-right" title="文本预处理">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="../../layers/writing-your-own-keras-layers/" class="btn btn-neutral" title="编写你自己的层"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" style="cursor: pointer">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
          <a href="https://github.com/keras-team/keras-docs-zh/" class="fa fa-github" style="float: left; color: #fcfcfc"> GitHub</a>
      
      
        <span><a href="../../layers/writing-your-own-keras-layers/" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="../text/" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '../..';</script>
    <script src="../../js/theme.js" defer></script>
      <script src="../../search/main.js" defer></script>

</body>
</html>
